package web.controller.address_telefon;

import hibernate.pojo.User;
import hibernate.pojo_address_telefon.Telephone;
import hibernate.util.HibernateUtil;

import java.util.ArrayList;
import java.util.List;

import javax.faces.component.UIData;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.Transaction;
import org.hibernate.classic.Session;
import org.hibernate.criterion.Restrictions;

import web.controller.LoginController;

public class ListTelephonesController {

	
	private static final Log logger = LogFactory.getLog(LoginController.class);
	
	private List<Telephone> telephones;
	private Telephone telephone;
	private UIData telephonesTable;
	private String name;
	private String areaCode;
	private String phoneNumber;
	
	public ListTelephonesController() {
		// TODO Auto-generated constructor stub
	}

	public void setTelephones(List<Telephone> telephones) {
		this.telephones = telephones;
	}

	public List<Telephone> getTelephones() {
		
		FacesContext context = FacesContext.getCurrentInstance();
		String telephoneBookID = (String) context.getExternalContext().getSessionMap().get("telephoneBookID");
		int telephoneBookIDForDBSelection = 0;
		try {
			telephones = new ArrayList<Telephone>();
			telephoneBookIDForDBSelection = Integer.parseInt(telephoneBookID);
			Session session = HibernateUtil.getSessionFactory().openSession();
			Criteria criteria = session.createCriteria(Telephone.class);
			criteria.add(Restrictions.eq("telephoneBookID",telephoneBookIDForDBSelection));
			telephones = criteria.list();
		} catch (Exception exception) {
			logger.error("Kullanici listesi database den okunurken hata olustu!",exception);
		}
		
		if(telephones != null && telephones.size() > 0){
			return telephones;
		} else 
			return null;
	}
	
	public String goruntule(){
		String pageToReturn = "";
		try {
			
			setTelephone(((Telephone)getTelephonesTable().getRowData()));
			
			setAreaCode(getTelephone().getAreaCode());
			setName(getTelephone().getName());
			setPhoneNumber(getTelephone().getPhoneNumber());
			
			logger.info("Initial Telephone Values!");
			logger.info(getAreaCode());
			logger.info(getName());
			logger.info(getPhoneNumber());
			pageToReturn = "showTelephoneDetailPage";
		} catch (Exception exception) {
			logger.error("Secilen telefonu getirirken hata olustu!", exception);
		}
		return pageToReturn;
	}
	
	public Telephone getTelephone() {
		return telephone;
	}

	public void setTelephone(Telephone telephone) {
		this.telephone = telephone;
	}
	
	public String updateTelephoneInformation(){
		String pageToRedirect = "";
		
		logger.info("Updated Telephone Values!");
		logger.info(getAreaCode());
		logger.info(getName());
		logger.info(getPhoneNumber());
		
		try {
			
//			FacesContext context = FacesContext.getCurrentInstance();
//			user = (User)context.getExternalContext().getSessionMap().get("user");
//			user.setFirstName(getFirstName());
//			user.setLastName(getLastName());
//			user.setUserName(getUserName());
			
			telephone.setAreaCode(getAreaCode());
			telephone.setName(getAreaCode());
			telephone.setPhoneNumber(getPhoneNumber());
			
			Session session = HibernateUtil.getSessionFactory().openSession();
			Transaction transaction = session.beginTransaction();
			session.update(telephone);
			transaction.commit();
		
			pageToRedirect = "updateUserInfoSuccess";
		} catch (Exception exception) {
			logger.error("Error Occured During User Info Update :", exception);
		}		
		
		return pageToRedirect;
	}

	public void setTelephonesTable(UIData telephonesTable) {
		this.telephonesTable = telephonesTable;
	}

	public UIData getTelephonesTable() {
		return telephonesTable;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAreaCode() {
		return areaCode;
	}

	public void setAreaCode(String areaCode) {
		this.areaCode = areaCode;
	}

	public String getPhoneNumber() {
		return phoneNumber;
	}

	public void setPhoneNumber(String phoneNumber) {
		this.phoneNumber = phoneNumber;
	}


}
